import initcy from './initcy'
import drawTopo from './drawTopo'
import getPoint from './getPort'
import tools from './tools'
import Config from './config'
import eventBus from '../eventBus'
import selectPort from './selectPort'
import drawLink from './drawLink'
import addThreeLine from './addThreeLine'
import fetchJson from './fetchJson'
var initDouble = ''

function moved() {
    var x = '';
    var y = '';

    var tempX = ''
    var tempY = ''

    return function(opt) {
        tempX = x;
        tempY = y;
        var optt = {
                x: tempX,
                y: tempY
            }
            // console.log("old x , old y:", x, y)
        x = opt.x
        y = opt.y

        if (tempX == '') {
            return {
                mx: 0,
                my: 0
            }
        } else {
            return {
                mx: opt.x - optt.x,
                my: opt.y - optt.y
            }
        }

        //    return optt
    }

}
var mposi = moved()

export default {
    start() {
        // var el = document.getElementById('app')
        Config.topoD = tools.getCookie('topoD')
        Config.bizx = tools.getCookie('equipment_id_topo')
        initcy.init()
            // this.cyadddata()
        this.setEvent()
        drawTopo.draw(initcy.getcy())
        this.documentEvent()
        if (Config.topoD === 'yes') {
            eventBus.bus.$emit('hideAllBnt')
        }
    },
    documentEvent() {
        document.addEventListener('mousemove', function(e) {
            eventBus.bus.$emit('mouseMove', { event: e })
        })
    },

    setSelectNode(evt, el) {
        var nodes = this

        if (!evt.originalEvent.ctrlKey) {
            selectPort.clearSelect()
        }

        // eventBus.bus.$emit('selectContainer', nodes)

        selectPort.setSelect(el)
    },

    setEvent() {
        var cy = initcy.getcy()
        var dom = cy.getElementById('rose')
        var that = this
            /*
    cy.on('tap', 'node', function (evt) {
      var nodes = this
      selectPort.selectEl(this)
    })
*/
        cy.on('click', 'edge', function(event) {
            console.log('this.data kkkkkk jjjjjjjj:', this.data())
            var data = this.data()
            eventBus.bus.$emit('getNodePathArr', data)
        })

        cy.on('tapstart', 'node', function(evt) {
            console.log("evt", evt)
                // that.setSelectNode(evt, this)
            mposi({ x: '', y: '' })
        })

        cy.on('mouseover', 'node', function(evt) {
            var that = this
            eventBus.bus.$emit('showHideNodeTip', {
                show: true,
                vm: that,
                x: evt.cyPosition.x,
                y: evt.cyPosition.y
            })
        })

        cy.on('mouseout', 'node', function(evt) {
            eventBus.bus.$emit('showHideNodeTip', {
                show: false
            })
        })

        cy.on('mouseover', 'edge', function(evt) {
            var that = this
            eventBus.bus.$emit('showHideEdgesTip', {
                show: true,
                vm: that,
                x: evt.cyPosition.x,
                y: evt.cyPosition.y
            })
        })

        cy.on('click', 'node', function(event) {

            that.setSelectNode(event, this)

        })

        cy.on('mouseout', 'edge', function(evt) {
            eventBus.bus.$emit('showHideEdgesTip', {
                show: false
            })
        })

        var tappedBefore
        var tappedTimeout

        cy.on('tap', function(event) {
            var tappedNow = event.cyTarget
                // console.log("thi shtis:", this)
                // that.setSelectNode(event, this)
            if (tappedTimeout && tappedBefore) {
                clearTimeout(tappedTimeout)
            }
            if (tappedBefore === tappedNow) {
                tappedNow.trigger('doubleTap')
                tappedBefore = null
            } else {
                tappedTimeout = setTimeout(function() { tappedBefore = null }, 300)
                tappedBefore = tappedNow
            }
        })

        cy.on('doubleTap', 'node', function(event) {
            var n = this.data()
            var nodeId = n.id
            var _url = '/topology/inforeticlearound/?equipment_id=' + nodeId + '&skip_dis_cab=1'

            if (!Config.dStatus) {
                eventBus.bus.$emit('message', "请在隐藏配线架模式下调整!")
                return
            }
            if (n.type == 'switch' || n.type == 'safety') {

                fetchJson.getJson({
                    self: this,
                    url: _url,
                    callback: function(data) {
                        drawTopo.addNextNode(data, nodeId)
                    }
                })
            } else {
                eventBus.bus.$emit('message', "请选择网络设备或者安全设备!")
                return
            }

        })

        cy.on("drag", "node", function(evt) {
            var that = this
            var el = this.data()

            var pos = this.position()
            var movXY = mposi(pos)

            addThreeLine.drag(this, movXY)

        })

        cy.on('cxttap', 'node', function(evt) {
            var data = this.data()
            var flag = false
            if (this.hasClass('selected')) {
                this.removeClass('selected')
            } else {
                flag = true
                this.addClass('selected')
            }
            drawLink.find(data, flag)
        })

        cy.on('click', 'node', function(event) {
            // drawLink.find(this.data(), true)
        })
    },
    cyadddata() {
        var cy = initcy.getcy()
        cy.add({
            group: 'edges',
            data: { source: 'aphid', target: 'grasshopper', label: '线1' },
            position: { x: 0, y: 0 }
        })
        setTimeout(function() {
            cy.viewport({
                pan: { x: 100, y: 100 }
            })
        }, 2000)
    }
}